package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.ChoosePhotosAda;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.utils.Utils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

public class ChoosePhotosActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.gv_choose_photo)
    GridView gvChoosePhoto;
    private ArrayList<String> photos = new ArrayList<>();

    private ChoosePhotosAda choosePhotosAda;
    private int num;
    private int what;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_photos);
        ButterKnife.bind(this);
        photos = getIntent().getStringArrayListExtra("photos");
        num=getIntent().getIntExtra("num",5);
        what=getIntent().getIntExtra("what",0);
        initView();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_enclosure, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (what){
                    case 0:
                        EventBus.getDefault().post(new MainEvent(Utils.EVENTMSG, photos));
                        break;
                    case 1:
                        EventBus.getDefault().post(new MainEvent("id", photos));
                        break;
                    case 2:
                        EventBus.getDefault().post(new MainEvent("company", photos));
                        break;
                    case 3:
                        EventBus.getDefault().post(new MainEvent("certify", photos));
                        break;
                }
                onBackPressed();
            }
        });
        gvChoosePhoto.setOnItemClickListener(this);
        choosePhotosAda = new ChoosePhotosAda(this, photos,what);
        gvChoosePhoto.setAdapter(choosePhotosAda);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == Utils.REQUEST_CODE) {
            if (data != null) {
                photos.clear();
                photos.addAll(data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS));
                choosePhotosAda = new ChoosePhotosAda(this, photos,what);
                gvChoosePhoto.setAdapter(choosePhotosAda);
//                choosePhotosAda.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (photos.size() == 0 || position == photos.size()) {
            PhotoPickerIntent intent = new PhotoPickerIntent(this);
            intent.setPhotoCount(num);
            intent.setShowCamera(true);
            startActivityForResult(intent, Utils.REQUEST_CODE);
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (what){
                case 0:
                    EventBus.getDefault().post(new MainEvent(Utils.EVENTMSG, photos));
                    break;
                case 1:
                    EventBus.getDefault().post(new MainEvent("id", photos));
                    break;
                case 2:
                    EventBus.getDefault().post(new MainEvent("company", photos));
                    break;
                case 3:
                    EventBus.getDefault().post(new MainEvent("certify", photos));
                    break;
            }
            onBackPressed();
        }
        return false;
    }
}
